# import requests
#
# response = requests.get('https://httpbin.org/get?age=5', proxies={
#     'http':'',
#     'https':''
# })

# import requests
#
# # 提取代理API接口，获取1个代理IP
# api_url = "https://dps.kdlapi.com/api/getdps/?secret_id=ohmnl0ol8ughjereeaha&signature=jad9hu9je0xaituz7598sn2yzldplx0u&num=1&sep=1"
#
# # 获取API接口返回的代理IP
# proxy_ip = requests.get(api_url).text
#
# # 用户名密码认证(私密代理/独享代理)
# username = "d4324721599"
# password = "zrgaj7al"
# proxies = {
#     "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
#     "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
# }
#
#
# # 要访问的目标网页
# target_url = "https://httpbin.org/get"
#
# # 使用代理IP发送请求
# response = requests.get(target_url, proxies=proxies)
#
# # 获取页面内容
# if response.status_code == 200:
#     print(response.text)







#!/usr/bin/env Python
# -*- coding: utf-8 -*-


"""
使用aiohttp请求代理服务器
请求http和https网页均适用
"""


import random
import asyncio
# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) windows系统请求https网站报错时调用此方法

import aiohttp
import requests

# API接口，返回格式为json
api_url = "https://dps.kdlapi.com/api/getdps/?secret_id=ohmnl0ol8ughjereeaha&signature=jad9hu9je0xaituz7598sn2yzldplx0u&num=5&format=json&sep=1"  # API接口

# API接口返回的proxy_list
proxy_list = requests.get(api_url).json().get('data').get('proxy_list')

# 用户名密码认证(私密代理/独享代理)
username = "d4324721599"
password = "zrgaj7al"

proxy_auth = aiohttp.BasicAuth(username, password)

page_url = "https://httpbin.org/get"  # 要访问的目标网页

async def fetch(session, url):
        async with session.get(url, proxy="http://" + random.choice(proxy_list), proxy_auth=proxy_auth) as resp:
            content = await resp.read()
            print(f"status_code: {resp.status}, content: {content}")


async def run():
    async with aiohttp.ClientSession() as session:
    # 异步发出5次请求
        tasks = [fetch(session, page_url) for _ in range(1000)]
        await asyncio.gather(*tasks)


if __name__ == '__main__':
    asyncio.run(run())
